{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 装饰器"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cake\n",
      "cake candles\n"
     ]
    }
   ],
   "source": [
    "# Decorator\n",
    "def add_candles(cake_func):\n",
    "    def insert_candles():\n",
    "        return cake_func() + \" candles\"\n",
    "    return insert_candles\n",
    "\n",
    "def make_cake():\n",
    "    return \"cake\"\n",
    "\n",
    "gift_func = add_candles(make_cake)\n",
    "\n",
    "print(make_cake())\n",
    "print(gift_func())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "cake candles\n"
     ]
    }
   ],
   "source": [
    "# 下面使用装饰器的效果等同于上面代码块\n",
    "def add_candles(cake_func):\n",
    "    def insert_candles():\n",
    "        return cake_func() + \" candles\"\n",
    "    return insert_candles\n",
    "\n",
    "@add_candles\n",
    "def make_cake():\n",
    "    return \"cake\"\n",
    "\n",
    "print(make_cake())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
